/*
  Tên:Trần Hoàng Lâm
  MSSV:11379951
  Lớp:DHTH7DLT
  Ngày hoàn thành:28/04/2012
  */
#include "frmphieunhapsach.h"
#include "ui_frmphieunhapsach.h"
#include "frsach.h"
#include "QStandardItem"



frmPhieuNhapSach::frmPhieuNhapSach(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frmPhieuNhapSach)
{
    ui->setupUi(this);
    //Khởi tạo
    DB =new MySqlConnection();
    ui->dtpNgay->setDate(QDate::currentDate());
    dataChiTietHD=new QStandardItemModel();
    trangThai=false;
    doiButton(false);
    //Gán sự kiện
    this->connect(ui->btnThemHD,SIGNAL(clicked()),this,SLOT(themHD()));
    this->connect(ui->btnXoaHD,SIGNAL(clicked()),this,SLOT(xoaHD()));
    this->connect(ui->btnSuaHD,SIGNAL(clicked()),this,SLOT(suaHD()));
    this->connect(ui->btnThoat,SIGNAL(clicked()),this,SLOT(thoat()));
    this->connect(ui->btnThemCT,SIGNAL(clicked()),this,SLOT(themChiTiet()));
   // this->connect(ui->btnSuaCT,SIGNAL(clicked()),this,SLOT(suaChiTiet()));
    this->connect(ui->btnXoaCT,SIGNAL(clicked()),this,SLOT(xoaChiTiet()));
    this->connect(ui->txtSL,SIGNAL(editingFinished()),this,SLOT(kiemTraSLNhap()));
    this->connect(ui->txtMaSach,SIGNAL(textChanged(QString)),SLOT(eventTextChangeMaSach()));
    this->connect(ui->dtpNgay,SIGNAL(dateChanged(QDate)),SLOT(eventSelectDateChange()));
    this->connect(ui->btnXem,SIGNAL(clicked()),this,SLOT(TimHoaDon()));
    this->connect(ui->btnMoformSach,SIGNAL(clicked()),this,SLOT(loadformSach()));
    //Hàm load khởi động
    resetTableSelectionEvent();
    //Số lượng nhập tối thiếu
    /*
      id 1 so luong nhap toi thieu
         2 so luong ton toi thieu nhap
         3 so tien no toi da
         4 so luong ton toi thieu ban
      */
    nhapToiThieu=DB->giatTriQuyDinh(1,quiDinh);
    ui->txtSL->setValue(nhapToiThieu);
}

frmPhieuNhapSach::~frmPhieuNhapSach()
{
    delete ui;
}

void frmPhieuNhapSach::doiButton(bool b)
{
 //Hóa đơn
    ui->btnThemHD->setEnabled(!b);
    //ui->btnSuaHD->setEnabled(!b);
    //ui->btnXoaHD->setEnabled(!b);

 //Chi tiết hóa đơn
    //ui->btnSuaCT->setEnabled(b);
    ui->btnXoaCT->setEnabled(b);
    ui->btnThemCT->setEnabled(b);
    b==true?ui->btnSuaHD->setText(trUtf8("Lưu")):ui->btnSuaHD->setText(trUtf8("Sửa"));
    b==true?ui->btnXoaHD->setText(trUtf8("Không lưu")):ui->btnXoaHD->setText(trUtf8("Xóa"));
 //tbl
    ui->tbviewHD->setEnabled(!b);

}

void frmPhieuNhapSach::themHD()
{
    bool tl=false;
    doiButton(true);
    DB->Open();
    QSqlQuery query(DB->DB);
    m_MaHD="N"+DB->sinhMa("h2l_HoaDonNhap","maHD");
    query.prepare("INSERT INTO h2l_HoaDonNhap (MaHD,NgayLap) VALUES (:mahd,:ngaylap)");
    query.bindValue(":mahd",m_MaHD);
    query.bindValue(":ngaylap",ui->dtpNgay->date());
    tl= query.exec();
    DB->Close();
    if(!tl)
        QMessageBox::information(this,trUtf8("Có lỗi xảy ra"),query.lastError().text());
    else
        resetTableSelectionEvent();
    //Sử lý chi tiết hóa đơn
     dataChiTietHD=new QStandardItemModel();
     ui->tbviewCTHD->setModel(dataChiTietHD);
     trangThai=true;


}

void frmPhieuNhapSach::suaHD()
{
    //Lưu khi thêm
    int tl=false;

    if(ui->btnSuaHD->text()==trUtf8("Lưu"))
    {
        if(trangThai)

        {
            QList<QStandardItem *> lstMaSach=dataChiTietHD->takeColumn(0);
            QList<QStandardItem *> lstSoLuong=dataChiTietHD->takeColumn(3);
            int soluong=lstMaSach.count();
            QStringList lstmaHD;
            QStringList qslstMaSach;
            QStringList qslstSoLuong;
            for(int i=0;i<soluong;i++)
            {
                lstmaHD+=m_MaHD;
                qslstMaSach+=lstMaSach.value(i)->text();
                qslstSoLuong+=lstSoLuong.value(i)->text();
            }


            DB->Open();
            QSqlQuery query(DB->DB);
            query.prepare("Insert into h2l_ChiTietHoaDonNhap set MaHD=?,MaSach=?,SoLuong=?");

            query.addBindValue(lstmaHD);
            query.addBindValue(qslstMaSach);
            query.addBindValue(qslstSoLuong);
            tl= query.execBatch();
            if(!tl)
                QMessageBox::information(this,trUtf8("Có lỗi xảy ra"),query.lastError().text());

            trangThai=false;
            DB->Close();
            loadChiTietHD(m_MaHD);
            doiButton(false);

        }//end lưu khi them
        else //Luu khi sua
        {
            //Xu ly xoa
            QStringList lstmaHD;
            int c=lstMaSachXoa.count();

            if(c>0)
            {
                for(int i=0;i<c;i++)
                    lstmaHD+=m_MaHD;
                QSqlQuery query(DB->DB);
                query.prepare("Delete from h2l_ChiTietHoaDonNhap where maHD=? and maSach=?");
                query.addBindValue(lstmaHD);
                query.addBindValue(lstMaSachXoa);
                tl= query.execBatch();
                if(!tl)
                    QMessageBox::information(this,trUtf8("Có lỗi xảy ra khi Delete"),query.lastError().text());
                lstMaSachXoa.clear();
                doiButton(false);
                return;

            }


            //End xử lý xóa

            //Xử lý insert và update
            QStringList lstmaHDUdate;
            QStringList lstMaSachUpdate;
            QStringList lstSoLuongUpdate;
            QStringList lstmaHDInsert;
            QStringList lstMaSachInsert;
            QStringList lstSoLuongInsert;
            QList<QStandardItem *> lstMaSach=dataChiTietHD->takeColumn(0);
            QList<QStandardItem *> lstSoLuong=dataChiTietHD->takeColumn(3);
            c=lstMaSach.length();
            //cách làm dỡ :(
            if(c>0)
            {
                for(int i=0;i<c;i++)
                {
                    QString msach=lstMaSach.value(i)->text();
                    QString soluong=lstSoLuong.value(i)->text();
                    int kt=kiemTraCTHD(m_MaHD,msach,soluong);
                    if(kt!=-1)//Tồn tại =>update
                    {
                        if(kt==0)
                        {
                            lstmaHDUdate+=m_MaHD;
                            lstMaSachUpdate+=msach;

                            lstSoLuongUpdate+=soluong;
                        }
                    }
                    else
                    {
                        lstmaHDInsert+=m_MaHD;
                        lstMaSachInsert+=msach;
                        lstSoLuongInsert+=soluong;
                    }

                }
                //Luu vao db
                //update
                if(lstmaHDUdate.length()>0)
                {
                    QSqlQuery query(DB->DB);
                    query.prepare("Update h2l_ChiTietHoaDonNhap set SoLuong =? where maHD=? and maSach=?");
                    query.addBindValue(lstSoLuongUpdate);
                    query.addBindValue(lstmaHDUdate);
                    query.addBindValue(lstMaSachUpdate);
                    tl= query.execBatch();
                    if(!tl)
                        QMessageBox::information(this,trUtf8("Có lỗi xảy ra khi Update"),query.lastError().text());
                }


                //Insert
                if(lstmaHDInsert.length()>0)
                {
                    QSqlQuery query(DB->DB);
                    query.prepare("INSERT INTO h2l_ChiTietHoaDonNhap(MaHD,MaSach,SoLuong) VALUES(?,?,?)");
                    query.addBindValue(lstmaHDInsert);
                    query.addBindValue(lstMaSachInsert);
                    query.addBindValue(lstSoLuongInsert);
                    tl= query.execBatch();
                    if(!tl)
                        QMessageBox::information(this,trUtf8("Có lỗi xảy ra khi Insert"),query.lastError().text());
                }
                doiButton(false);
                loadChiTietHD(m_MaHD);
                return;
            }//cach lam do

        }//end xóa
    }else//Nut Sửa
    {


        doiButton(true);
    }//end sua




}

void frmPhieuNhapSach::xoaHD()
{
    //Không lưu
    bool fag=false;
    bool tl;
    if(ui->btnXoaHD->text()==trUtf8("Không lưu"))
    {
        if(trangThai)
        {
            fag=true;
            if(!fag)
                if(QMessageBox::information(this,trUtf8("Thông báo"),trUtf8("Bạn muốn xóa hóa đơn mã ")+m_MaHD+trUtf8(" ra khỏi danh sách ?"),QMessageBox::Yes,QMessageBox::No)==QMessageBox::No)
                {
                    ui->tbviewCTHD->reset();//Xóa nội dung
                    return;
                }

            DB->Open();
            QSqlQuery query(DB->DB);
            query.prepare("DELETE FROM h2l_HoaDonNhap WHERE maHD=:maHD");
            query.bindValue(":maHD",m_MaHD);
            tl= query.exec();
            DB->Close();
            if(!tl)
            {
                QMessageBox::information(this,trUtf8("Có lỗi xảy ra"),query.lastError().text());
                return;
            }
            //Xoa noi dung tbl chi tiet
            resetTableSelectionEvent();
            if(m_rowIndex>0)
                ui->tbviewHD->selectRow(m_rowIndex-1);

        }
        else
        {

        }
    }
    else
    {
        if(QMessageBox::information(this,trUtf8("Thông báo"),trUtf8("Bạn muốn xóa hóa đơn mã ")+m_MaHD+trUtf8(" ra khỏi danh sách ?"),QMessageBox::Yes,QMessageBox::No)==QMessageBox::No)
            return;
        DB->Open();
        QSqlQuery query(DB->DB);
        query.prepare("DELETE FROM h2l_HoaDonNhap WHERE maHD=:maHD");
        query.bindValue(":maHD",m_MaHD);
        tl= query.exec();
        DB->Close();
        if(!tl)
        {
            QMessageBox::information(this,trUtf8("Có lỗi xảy ra"),query.lastError().text());
            return;
        }
    }

    //Xoa noi dung tbl chi tiet
    resetTableSelectionEvent();
    if(m_rowIndex>0)
        ui->tbviewHD->selectRow(m_rowIndex-1);
    doiButton(false);


}

void frmPhieuNhapSach::thoat()
{

    this->close();
}

void frmPhieuNhapSach::themChiTiet()
{
    if(ui->txtMaSach->text().trimmed().isEmpty()||ui->txtTenSach->text().isEmpty())
    {
        QMessageBox::warning(this,trUtf8("Thông tin"),trUtf8("Vui lòng kiểm tra nhập lại mã sách"));
        return;
    }
    quydinhnhapton=DB->giatTriQuyDinh(2,quiDinh);
    if(quiDinh)
    {
        int soluongton=0;
        QSqlQuery query=DB->exec("select SoLuongTon from Sach where MaSach="+ui->txtMaSach->text());
        if(query.isSelect())
        {
            query.first();
            soluongton=query.value(0).toInt();
        }
        if(soluongton>quydinhnhapton)
        {
            QMessageBox::information(this,trUtf8("Thông báo"),trUtf8("Số lượng tồn trong kho phải ít hơn ")+QString::number(quydinhnhapton)+trUtf8(" theo quy định"));
            return;
        }
    }


    indexCTHD=dataChiTietHD->rowCount();
    //Kiểm tra đã có trong danh sách
    QList<QStandardItem *> tim= dataChiTietHD->findItems(ui->txtMaSach->text(),Qt::MatchExactly,0);

    if(tim.count()==1)
    {
        QModelIndex index=tim.first()->index();
        int vt=index.row();
        QAbstractItemModel *item =ui->tbviewCTHD->model();
        QString curentValue=item->data(item->index(vt,4)).toString();
        if(QMessageBox::information(this,trUtf8("Thông báo"),trUtf8("Mã sách ")+ui->txtMaSach->text()+trUtf8(" này đã có trong danh sách với số lượng là ")+curentValue+trUtf8("\nBạn có muốn cập nhật lại không ?"),QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes)
        {

            QStandardItem *soluong = new QStandardItem(QString(QString::number(ui->txtSL->value())));
            dataChiTietHD->setItem(vt, 4,soluong);
            return;
        }
        else
            return;
    }
    //end kiểm tra

    QStandardItem *masach = new QStandardItem(QString(ui->txtMaSach->text()));
    QStandardItem *tensach = new QStandardItem(QString(ui->txtTenSach->text()));
    QStandardItem *tacgia = new QStandardItem(ui->txtTacGia->text());
    QStandardItem *theloai = new QStandardItem(QString(ui->txtTheLoai->text()));
    QStandardItem *soluong = new QStandardItem(QString(QString::number(ui->txtSL->value())));
    dataChiTietHD->setItem(indexCTHD, 0,masach);
    dataChiTietHD->setItem(indexCTHD, 1,tensach);
    dataChiTietHD->setItem(indexCTHD, 2,tacgia);
    dataChiTietHD->setItem(indexCTHD, 3,theloai);
    dataChiTietHD->setItem(indexCTHD, 4,soluong);
    dataChiTietHD->setHeaderData(0, Qt::Horizontal,trUtf8("Mã sách"));
    dataChiTietHD->setHeaderData(1, Qt::Horizontal,trUtf8("Tên Sách"));
    dataChiTietHD->setHeaderData(2, Qt::Horizontal,trUtf8("Tác Giả"));
    dataChiTietHD->setHeaderData(3, Qt::Horizontal,trUtf8("Thể Loại"));
    dataChiTietHD->setHeaderData(4, Qt::Horizontal,trUtf8("Số Lượng"));   
    ui->tbviewCTHD->setModel(dataChiTietHD);
    ui->tbviewCTHD->setColumnWidth(1,250);
    ui->tbviewCTHD->setColumnWidth(2,200);
    ui->tbviewCTHD->setColumnWidth(3,100);
    ui->tbviewCTHD->setColumnWidth(4,100);
    ui->tbviewCTHD->setColumnHidden(0,true);    
    ui->tbviewCTHD->show();
    indexCTHD=indexCTHD+1;    
    //Sự kiện select
    if(trangThai)
    {
        selectionModelCT = ui->tbviewCTHD->selectionModel();
        connect(selectionModelCT, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(selectRowCTHD()));
    }


}

void frmPhieuNhapSach::xoaChiTiet()
{
    //Xóa chi tiết khi thêm mới

    if(trangThai)
    {
        if(dataChiTietHD->rowCount()==1)
            dataChiTietHD->removeRow(0);
        dataChiTietHD->removeRow(indexCTHD);
    }

    else
    {
        //Xóa khi sửa
        if(!ui->txtMaSach->text().trimmed().isEmpty())
        {

            if(kiemTraCTHD(m_MaHD,ui->txtMaSach->text(),QString::number(ui->txtSL->value()))>0)
            {
                lstMaSachXoa+=ui->txtMaSach->text();
                dataChiTietHD->removeRow(indexCTHD);
            }
        }
    }


}

void frmPhieuNhapSach::suaChiTiet()
{
    QAbstractItemModel *item =ui->tbviewCTHD->model();
    QString curentValue=item->data(item->index(indexCTHD,4)).toString();
    QString sl=QString::number(ui->txtSL->value());
    if(curentValue!=sl)
    {
        if(QMessageBox::information(this,trUtf8("Thông báo"),trUtf8("Bạn vừa sửa mã sách ")+ui->txtMaSach->text()+trUtf8(" với số lượng nhập ")+sl+trUtf8(" \nBạn có muốn lưu thay đổi lại không ?"),QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes)
        {

            QStandardItem *soluong = new QStandardItem(QString(QString::number(ui->txtSL->value())));
            dataChiTietHD->setItem(indexCTHD, 4,soluong);
            return;
        }
        else
            return;
    }
}

void frmPhieuNhapSach::loadHoaDon()
{

        model = DB->getTable("select * from h2l_HoaDonNhap where NgayLap='"+ui->dtpNgay->text()+"'");
        model->setHeaderData(0, Qt::Horizontal,trUtf8("Mã hóa đơn"));
        model->setHeaderData(1, Qt::Horizontal,trUtf8("Ngày lập"));
        ui->tbviewHD->setModel(model);
        ui->tbviewHD->setItemDelegateForColumn(1,new DateFormatDelegate("dd/MM/yyyy"));
        ui->tbviewHD->show();
}

void frmPhieuNhapSach::resetTableSelectionEvent()
{
    loadHoaDon();
    selectionModel = ui->tbviewHD->selectionModel();
    connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(selectRowHD()));
}

void frmPhieuNhapSach::selectRowHD()
{
    QAbstractItemModel *item =ui->tbviewHD->model();
    QModelIndex index = ui->tbviewHD->currentIndex();
    m_rowIndex = index.row();
    m_MaHD=item->data(item->index(m_rowIndex,0)).toString();
    loadChiTietHD(m_MaHD);
    ui->txtMaSach->clear();    
}

void frmPhieuNhapSach::loadChiTietHD(QString MaHD)
{
    indexCTHD=0;
    dataChiTietHD=new QStandardItemModel();
    QSqlQuery query=DB->exec("SELECT s.MaSach,s.TenSach,t.TenTheLoai,tg.TenTacGia,ct.SoLuong FROM h2l_ChiTietHoaDonNhap ct,Sach s,TheLoai t,TacGia tg where ct.MaSach=s.MaSach and t.MaTheLoai=s.MaTheLoai and tg.MaTacGia=s.MaTacGia and ct.MaHD='"+MaHD+"'");
    if(query.size()>0)
    while(query.next())
    {
        QStandardItem *masach = new QStandardItem(QString(query.value(0).toString()));
        QStandardItem *tensach = new QStandardItem(QString(query.value(1).toString()));
        QStandardItem *theloai = new QStandardItem(QString(query.value(2).toString()));
        QStandardItem *tacgia = new QStandardItem(QString(query.value(3).toString()));
        QStandardItem *soluong = new QStandardItem(QString(query.value(4).toString()));
        dataChiTietHD->setItem(indexCTHD, 0,masach);
        dataChiTietHD->setItem(indexCTHD, 1,tensach);
        dataChiTietHD->setItem(indexCTHD, 2,tacgia);
        dataChiTietHD->setItem(indexCTHD, 3,theloai);
        dataChiTietHD->setItem(indexCTHD, 4,soluong);
        indexCTHD++;
    }
    dataChiTietHD->setHeaderData(0, Qt::Horizontal,trUtf8("Mã sách"));
    dataChiTietHD->setHeaderData(1, Qt::Horizontal,trUtf8("Tên Sách"));
    dataChiTietHD->setHeaderData(2, Qt::Horizontal,trUtf8("Tác Giả"));
    dataChiTietHD->setHeaderData(3, Qt::Horizontal,trUtf8("Thể Loại"));
    dataChiTietHD->setHeaderData(4, Qt::Horizontal,trUtf8("Số Lượng"));
    ui->tbviewCTHD->setModel(dataChiTietHD);    
    ui->tbviewCTHD->setColumnWidth(1,250);
    ui->tbviewCTHD->setColumnWidth(2,200);
    ui->tbviewCTHD->setColumnWidth(3,100);
    ui->tbviewCTHD->setColumnWidth(4,100);
    ui->tbviewCTHD->setColumnHidden(0,true);

    selectionModelCT = ui->tbviewCTHD->selectionModel();
    connect(selectionModelCT, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(selectRowCTHD()));

//    qModelCTHD = DB->getTable("SELECT s.TenSach,t.TenTheLoai,tg.TenTacGia,ct.SoLuong FROM h2l_ChiTietHoaDonNhap ct,Sach s,h2l_TheLoai t,TacGia tg where ct.MaSach=s.MaSach and t.MaTheLoai=s.MaTheLoai and tg.MaTacGia=s.MaTacGia and ct.MaHD='"+MaHD+"'");
//    qModelCTHD->setHeaderData(0, Qt::Horizontal,trUtf8("Tên Sách"));
//    qModelCTHD->setHeaderData(1, Qt::Horizontal,trUtf8("Tác Giả"));
//    qModelCTHD->setHeaderData(2, Qt::Horizontal,trUtf8("Thể Loại"));
//    qModelCTHD->setHeaderData(3, Qt::Horizontal,trUtf8("Số Lượng"));
//    ui->tbviewCTHD->setModel(qModelCTHD);
//    ui->tbviewCTHD->show();

}

void frmPhieuNhapSach::kiemTraSLNhap()
{
    nhapToiThieu=DB->giatTriQuyDinh(1,quiDinh);
    if(quiDinh)
        if(nhapToiThieu>ui->txtSL->value())
        {
            QMessageBox::information(this,trUtf8("Thông báo"),trUtf8("Số lượng nhập phải lớn hơn hoặc bằng ")+QString::number(nhapToiThieu)+trUtf8(" theo quy định"));
            ui->txtSL->setValue(nhapToiThieu);
        }
}

void frmPhieuNhapSach::loadChiTietSach(int MaSach)
{

    QSqlQuery query=DB->exec("SELECT s.TenSach,tg.TenTacGia,l.TenTheLoai FROM Sach s,TacGia tg,TheLoai l where s.MaTacGia=tg.MaTacGia and s.MaTheLoai=l.MaTheLoai and s.MaSach="+QString::number(MaSach));
    if(query.first()&&query.size()==1)
    {
    ui->txtTenSach->setText(query.value(0).toString());
    ui->txtTacGia->setText(query.value(1).toString());
    ui->txtTheLoai->setText(query.value(2).toString());
    }
    else
    {
        ui->txtTenSach->clear();
        ui->txtTacGia->clear();
        ui->txtTheLoai->clear();
    }





}

void frmPhieuNhapSach::eventTextChangeMaSach()
{
    loadChiTietSach(ui->txtMaSach->text().toInt());
    QList<QStandardItem *> tim= dataChiTietHD->findItems(ui->txtMaSach->text(),Qt::MatchExactly,0);
    if(tim.count()==1)
        ui->btnThemCT->setText(trUtf8("Sửa Chi Tiết HĐ"));
    else
        ui->btnThemCT->setText(trUtf8("Thêm vào Chi Tiết HĐ"));
}

void frmPhieuNhapSach::selectRowCTHD()
{
    QAbstractItemModel *item =ui->tbviewCTHD->model();
    QModelIndex index = ui->tbviewCTHD->currentIndex();
    indexCTHD = index.row();
    ui->txtMaSach->setText(item->data(item->index(indexCTHD,0)).toString());
    ui->txtSL->setValue(item->data(item->index(indexCTHD,4)).toInt());




}


int frmPhieuNhapSach::kiemTraCTHD(QString maHD, QString maSach, QString soLuong)
{
   //Kiem tra chi tiet hoa don ton tai
    QSqlQuery query=DB->exec("select SoLuong from h2l_ChiTietHoaDonNhap where maHD='"+maHD+"' and maSach='"+maSach+"'");
    query.first();
    if(query.size()==1)
    {
        if(query.value(0).toInt()==soLuong.toInt())
            return 1;//tồn tại ko thay đổi
        return 0;//tồi tại thay đổi
    }
    return -1;// ko tồn tại


}

void frmPhieuNhapSach::eventSelectDateChange()
{
    resetTableSelectionEvent();

}

void frmPhieuNhapSach::TimHoaDon()
{


    if(ui->btnXem->text()==trUtf8("Tìm"))
    {
        model = DB->getTable("select * from h2l_HoaDonNhap where MaHD='"+ui->txtMaHD->text()+"'");
        model->setHeaderData(0, Qt::Horizontal,trUtf8("Mã hóa đơn"));
        model->setHeaderData(1, Qt::Horizontal,trUtf8("Ngày lập"));
        ui->tbviewHD->setModel(model);
        ui->tbviewHD->setItemDelegateForColumn(1,new DateFormatDelegate("dd/MM/yyyy"));
        ui->tbviewHD->show();
        selectionModel = ui->tbviewHD->selectionModel();
        connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(selectRowHD()));

        ui->btnXem->setText(trUtf8("Thôi"));
    }
    else
    {

        ui->txtMaHD->clear();
        ui->btnXem->setText(trUtf8("Tìm"));
        ui->dtpNgay->setDate(QDate::currentDate());
        resetTableSelectionEvent();

    }



}

void frmPhieuNhapSach::loadformSach()
{
    frSach *frm=new frSach();
    frm->exec();
    ui->txtMaSach->setText(frm->selecteds);
}
